package com.css.apps.base.user.action.relate;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.css.apps.base.common.provider.UserQueryProvider;
import com.css.apps.base.user.action.UserAction;
import com.css.apps.base.user.model.SUser;
import com.css.apps.base.user.model.UserRelate;
import com.css.core.configuration.Environment;
import com.css.db.query.QueryCache;
import com.css.db.query.TransactionCache;
import com.css.util.Ajax;
import com.css.util.Messages;
import com.css.util.StringHelper;
import com.opensymphony.xwork.Action;

@SuppressWarnings("serial")
public class AddUserRelate extends UserAction {

	private static Log log = LogFactory.getLog(AddUserRelate.class);
	private String userId;
	private String relateId;
	private String relateType;
	private String relateOrgId;

	@Override
	protected String userGo() {
		TransactionCache tx = null;
		try {
			if (StringHelper.isEmpty(relateId) || StringHelper.isEmpty(relateType)
					|| StringHelper.isEmpty(relateOrgId)) {
				result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.chooseOne"));
				return Action.ERROR;
			}
			SUser sUser = UserQueryProvider.currentUser();

			String check = checkSameRelate(sUser.getUserId(), relateId, relateType, relateOrgId);
			if (StringHelper.isNotEmpty(check)) {
				result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.sameRelate"));
				return Action.ERROR;
			}

			// 保存功能
			UserRelate item = new UserRelate();
			item.setUserId(sUser.getUserId());
			item.setRelateId(relateId);
			item.setRelateType(relateType);
			item.setRelateOrgId(relateOrgId);
			item.setGroupId("0");
			tx = new TransactionCache();
			tx.save(item);
			tx.commit();

			result = Ajax.JSONResult(Environment.RESULT_CODE_SUCCESS, Messages.getString("systemMsg.success"));
			return Action.SUCCESS;

		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			if (tx != null) {
				tx.rollback();
			}
			result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.exception"));
			return Action.ERROR;
		}
	}

	private String checkSameRelate(String user, String relate, String type, String org) {
		QueryCache qc = new QueryCache(
				"select uuid from UserRelate where userId = :userId and relateId =:relateId and relateType =:relateType and relateOrgId =:relateOrgId ")
						.setParameter("userId", user).setParameter("relateId", relate).setParameter("relateType", type)
						.setParameter("relateOrgId", org).setMaxResults(1);
		String same = (String) qc.uniqueResultCache();
		return same;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getRelateId() {
		return relateId;
	}

	public void setRelateId(String relateId) {
		this.relateId = relateId;
	}

	public String getRelateType() {
		return relateType;
	}

	public void setRelateType(String relateType) {
		this.relateType = relateType;
	}

	public String getRelateOrgId() {
		return relateOrgId;
	}

	public void setRelateOrgId(String relateOrgId) {
		this.relateOrgId = relateOrgId;
	}

}
